package com.wanlian.dao.impl;

import com.wanlian.dao.QuestionAnswerDao;
import com.wanlian.entity.QuestionAnswer;
import com.wanlian.model.ResultModel;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class QuestionAnswerDaoImpl extends GenericDaoImpl<QuestionAnswer> implements QuestionAnswerDao {
    public QuestionAnswerDaoImpl() {
        super(QuestionAnswer.class);
    }

    @Override
    public ResultModel queryQuestionAnswer(Page page, Map<String, Object> conditions) {
            StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
            ArrayList<Object> param = new ArrayList<Object>();
            if (conditions != null) {
                if (conditions.containsKey("questionId")) {
                    int questionId = (int) conditions.get("questionId");
                        sqlWhere.append(" and qid =? ");
                        param.add( questionId  );
                }
                if (conditions.containsKey("lastUpdataTime")) {
                    try {
                        String lastUpdataTime = (String) conditions.get("lastUpdataTime");
                        if (StringUtil.hasValue(lastUpdataTime  )) {
                            sqlWhere.append(" and lastUpdataTime=date(?)");
                            param.add(lastUpdataTime);
                        }
                    } catch (Exception e) {
                        System.out.println("为非数字" + e.toString());
                    }

                }
            }
            List<QuestionAnswer> lists = new ArrayList<QuestionAnswer>();
            String hql = String.format("from %s " + sqlWhere, entityClassName());
            lists = this.queryForListByPage(hql, param.toArray(), page);
            String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
            long count = this.queryCount(countSql, param.toArray());
            ResultModel resultModel = new ResultModel(true, lists, count);
            return resultModel;
    }


    @Override
    public long queryCountById(int id) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 and qid=? ");
        ArrayList<Object> param = new ArrayList<Object>();
        param.add(id);
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());

        return count;
    }
}
